/**
 * 
 */
package com.roc.dubbo.boot.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.util.StringUtils;

/**
 * @author "Ren PengFei (ROC)"
 * @date 2016年4月7日 下午4:36:19
 * @description
 */
@Configuration
@EnableConfigurationProperties(DubboProperties.class)
public class DubboBasicConfiguration {

	static com.alibaba.dubbo.common.logger.Logger logger = LoggerFactory.getLogger("BootDubboConfiguration");

	static {
		logger.info("Dubbo & Spring Boot 配置");
	}

	@Autowired
	private DubboProperties dubboProperties;

	@Bean
	public ApplicationConfig applicationConfig() {
		// 当前应用配置
		ApplicationConfig applicationConfig = new ApplicationConfig();
		logger.info("注册：" + dubboProperties);
		applicationConfig.setName(dubboProperties.getApplicationName());
		applicationConfig.setOrganization(dubboProperties.getApplicationOrganizatione());
		applicationConfig.setOwner(dubboProperties.getApplicationOwner());
		applicationConfig.setRegistry(registryConfig());
		applicationConfig.setMonitor(monitorConfig());
		return applicationConfig;
	}

	//
	@Bean
	public RegistryConfig registryConfig() {
		RegistryConfig registryConfig = new RegistryConfig();
		registryConfig.setProtocol(dubboProperties.getRegistryProtocol());
		registryConfig.setAddress(dubboProperties.getRegistryAddress());
		registryConfig.setFile(dubboProperties.getRegistryFile());
		registryConfig.setClient(dubboProperties.getRegistryClient());
		return registryConfig;
	}

	//
	@Bean
	public ProtocolConfig protocolConfig() {
		// 服务提供者协议配置
		ProtocolConfig protocolConfig = new ProtocolConfig();
		protocolConfig.setName(dubboProperties.getProtocolName());
		protocolConfig.setPort(dubboProperties.getProtocolPort());
		return protocolConfig;
	}

	//
	@Bean
	public MonitorConfig monitorConfig() {
		MonitorConfig monitorConfig = new MonitorConfig();
		monitorConfig.setProtocol(dubboProperties.getMonitorProtocol());
		return monitorConfig;
	}

	//
	@Bean(name = "defaultProvider")
	public ProviderConfig providerConfig() {
		ProviderConfig providerConfig = new ProviderConfig();
		providerConfig.setTimeout(dubboProperties.getTimeout());
		providerConfig.setRetries(dubboProperties.getRetries());
		providerConfig.setGroup(dubboProperties.getProviderGroup());
		providerConfig.setLayer(dubboProperties.getProviderLayer());
		providerConfig.setDelay(dubboProperties.getDelay());
		providerConfig.setApplication(applicationConfig());
		providerConfig.setRegistry(registryConfig());
		return providerConfig;
	}

	//
	@Bean
	public ConsumerConfig consumerConfig() {
		ConsumerConfig consumerConfig = new ConsumerConfig();
		consumerConfig.setGroup(dubboProperties.getConsumerGroup());
		return consumerConfig;
	}

}
